package com.share.module.sys.menu.service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.share.module.sys.menu.dao.MenuDAO;
import com.share.module.sys.menu.entity.Menu;
import com.stock.common.lang.StringUtils;
import com.stock.mybatisplus.utils.PageUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 菜单 服务实现类
 * </p>
 *
 * @author lijy
 * @since 2023-04-17
 */
@Service
public class MenuService extends ServiceImpl<MenuDAO, Menu> {

    public PageUtils page(Integer current, Integer size, String name) {
        IPage<Menu> page = super.page(new Page<>(current, size), Wrappers.<Menu>lambdaQuery()
                .like(com.baomidou.mybatisplus.core.toolkit.StringUtils.isNotEmpty(name), Menu::getName, name)
                .orderByDesc(Menu::getId));
        return new PageUtils(page);
    }

    public List<Menu> findAll(String name) {
        List<Menu> list = list(Wrappers.<Menu>lambdaQuery().like(StringUtils.isNotBlank(name), Menu::getName, name));
        List<Menu> parentNode = list.stream().filter(s -> s.getPid() == 0).collect(Collectors.toList());
        // 一级菜单子菜单
        for (Menu menu : parentNode) {
            menu.setChildren(list.stream().filter(m -> menu.getId().equals(m.getPid())).collect(Collectors.toList()));
        }
        return parentNode;
    }

}
